package com.all.play.myapplication.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.all.play.myapplication.Bean.ProductBean;
import com.all.play.myapplication.R;
import com.all.play.myapplication.adapter.DatabaseManAdapter;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 聪明一只哈 on 2018/4/23.
 * Time: 2018/4/23  8:48
 */

public class DatabaseManActivity extends AppCompatActivity {

    @BindView(R.id.rv_db_manager)
    RecyclerView rvDbManager;
    private DatabaseManAdapter manAdapter;

    private int tag=0;
    private List<ProductBean> pbList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dbmanager);
        ButterKnife.bind(this);
        LitePal.getDatabase();


        initData();

    }

    /**
     * 加载RecycleView数据
     */
    private void initData() {
        pbList = DataSupport.findAll(ProductBean.class);
        Collections.reverse(pbList);
        manAdapter = new DatabaseManAdapter(DatabaseManActivity.this, pbList);

        rvDbManager.setAdapter(manAdapter);

        rvDbManager.setLayoutManager(new LinearLayoutManager(DatabaseManActivity.this));
    }

    /**
     * 数据库增加数据
     * @param view
     */
    public void add(View view) {

        //数据库的操作
        ProductBean productBean = new ProductBean();
        productBean.setName("hashiqi"+tag);
        productBean.setPages(200+tag);
        productBean.setFactory("DisneyLand"+tag);
        productBean.setPrice(100.0+(double) tag);
        productBean.save();
        //rv对数据的显示
        /*if (manAdapter!=null){
            pbList = DataSupport.findAll(ProductBean.class);
            manAdapter.notifyDataSetChanged();
        }*/
        initData();
        tag++;

    }

    /**
     * 数据库删除数据
     * @param view
     */
    public void delete(View view) {
        Toast.makeText(DatabaseManActivity.this, "全部删除", Toast.LENGTH_SHORT).show();
        DataSupport.deleteAll(ProductBean.class);
        initData();
        tag=0;
    }

    /**
     * 数据库更新数据
     * @param view
     */
    public void update(View view) {
       /* ProductBean prob = new ProductBean();
        prob.setPages(400);
        prob.updateAll("id=?", "3");*/

       //长按对应的条目,进行数据的修改
        Toast.makeText(DatabaseManActivity.this, "长按对应的条目,进行数据的修改", Toast.LENGTH_SHORT).show();
    }

    /**
     * 数据库查询数据
     * @param view
     */
    public void query(View view) {
        pbList = DataSupport.findAll(ProductBean.class);
        for (ProductBean pbl : pbList) {
            Log.i("querypblList", "query: " + pbl.getId() + "~" + pbl.getName() + "~" + pbl.getPages() + "~" + pbl.getPrice() + "~" + pbl.getFactory());

            Toast.makeText(DatabaseManActivity.this, "" + pbl.getId() + "~" + pbl.getName() + "~" + pbl.getPages() + "~" + pbl.getPrice() + "~" + pbl.getFactory(), Toast.LENGTH_SHORT).show();
        }

        initData();
    }
}
